load("//bazel/install_rules:install_rules.bzl", "extensions_with_config")
load("//bazel:mongo_src_rules.bzl", "mongo_cc_extension_shared_library")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
    ]),
)

# This sets up the valid extension targets to use in the top level `dist-test`
# and `extensions` targets. Using Bazel's transition system, this rule ensures
# that the extensions are built with the correct configuration. All extensions
# that may be loaded in jstests should be included here. Extensions that are
# built only for unit tests should be omitted.
extensions_with_config(
    name = "dist_test_extensions",
    srcs = [
        ##################### EXTENSIONS FOR PASSTHROUGH TESTS #####################
        # Any extension that we intend to load in extensions passthrough tests and in the
        # extension-enabled variant MUST have the "_mongo_extension" suffix for the resmoke
        # scripts to recognize it.

        # TODO SERVER-109838: Remove the comment below once extension stages are no longer
        # listed by $listMqlEntities.
        # Any extension added here will result in the with-extensions Evergreen variant
        # automatically loading it into the server for its tasks. This means that
        # null_chars_string_input.js will break as not all stages from $listMqlEntities
        # will have a null character test. To resolve this, ensure that these new stages
        # are added to the "skips" list in that test.
        ":bar_mongo_extension",
        ":foo_mongo_extension",
        ":logging_mongo_extension",
        ":debug_logging_mongo_extension",
        ":parse_options_mongo_extension",
        ":test_options_mongo_extension",
        ":toaster_mongo_extension",
        ":extension_errors_mongo_extension",
        ":shapify_mongo_extension",
        ":sharded_execution_serialization_mongo_extension",
        ":explain_mongo_extension",
        ":idle_threads_mongo_extension",
        ":interrupt_mongo_extension",
        ":match_topN_mongo_extension",
        ":native_vector_search_mongo_extension",
        ":metrics_mongo_extension",
        ":other_metrics_mongo_extension",

        #################### EXTENSIONS FOR NO-PASSTHROUGH TESTS ####################
        # Any extension that is just loaded in a no-passthrough test MUST NOT have the
        # "_mongo_extension" suffix.
        ":no_symbol_bad_extension",
        ":duplicate_stage_descriptor_bad_extension",
        ":foo_extension_v2",
        ":vector_search_extension",
    ],
)

pkg_name = "//" + package_name() + "/"

############################## EXTENSIONS FOR PASSTHROUGH TESTS ##############################
### Any extension name suffix'ed with "_mongo_extension" with be automatically picked up by
### the extension passthroughs and loaded on all nodes, if it is also added to
### "dist_test_extensions" in mongo/BUILD.bazel.

# Extensions under test_examples/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [extension_name + ".cpp"],
    )
    for extension_name in [
        "foo",
        "foo_v2",
        "bar",
    ]
]

# Extensions under test_examples/desugar/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [pkg_name + "desugar:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "match_topN",
        "native_vector_search",
    ]
]

# Extensions under test_examples/extension_options/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [pkg_name + "extension_options:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "test_options",
        "parse_options",
        "toaster",
    ]
]

# Extensions under test_examples/loading/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [pkg_name + "loading:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "host_version_succeeds",
        "load_highest_compatible_version",
        "load_two_stages",
    ]
]

# Extensions under test_examples/host_services/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [pkg_name + "host_services:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "logging",
        "extension_errors",
        "debug_logging",
        "idle_threads",
    ]
]

# Extensions under test_examples/observability/
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_mongo_extension",
        srcs = [pkg_name + "observability:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "sharded_execution_serialization",
        "explain",
        "shapify",
        "metrics",
        "other_metrics",
        "interrupt",
    ]
]

######################### EXTENSIONS FOR NO PASSTHROUGH OR UNIT TESTS #########################
### To be manually loaded in a no-passthrough e2e test, the extension must also be added to
### "dist_test_extensions" in mongo/BUILD.bazel.
### To be manually loaded in a unit test, the extension must also be added to the list of "data"
### for that test's Bazel target.

# Extensions under test_examples/fail_to_load/
# Each of these should fail startup.
[
    mongo_cc_extension_shared_library(
        name = extension_name + "_bad_extension",
        srcs = [pkg_name + "fail_to_load:" + extension_name + ".cpp"],
    )
    for extension_name in [
        "no_symbol",
        "null_mongo_extension",
        "major_version_too_high",
        "major_version_too_low",
        "minor_version_too_high",
        "null_initialize_function",
        "major_version_max_int",
        "duplicate_stage_descriptor",
        "host_version_fails",
        "initialize_version_fails",
        "null_stage_descriptor",
        "duplicate_version",
        "no_compatible_version",
    ]
]

# "foo_v2" is used for testing upgrade scenarios as the upgrade of "foo" from above. We cannot use
# the "mongo_extension" suffix since loading this extension in passthroughs with "foo" V1 should
# cause duplicate stage errors.
mongo_cc_extension_shared_library(
    name = "foo_extension_v2",
    srcs = ["foo_v2.cpp"],
)

# "vector_search" is used to test that we can override the existing $vectorSearch implementation.
# It must not have the _mongo_extension suffix so that it doesn't get loaded in the
# Extensions-enabled variant since that would break real $vectorSearch tests.
mongo_cc_extension_shared_library(
    name = "vector_search_extension",
    srcs = ["vector_search.cpp"],
)
